package com.smh.a02loginAndRegister;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Test2 {
    public static void main(String[] args) throws IOException {


        File file = new File("day28-code\\file\\user\\userinfo2.txt");

        BufferedReader br = new BufferedReader(new FileReader(file));
        ArrayList<UserInfo> list = new ArrayList<>();

        String line;
        while ((line = br.readLine()) != null) {
            String[] split = line.split("(&|=)");
            list.add(new UserInfo(split[1], split[3], Integer.parseInt(split[5])));
        }
        System.out.println(list);

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String name = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        int flag = 0;
        for (UserInfo userInfo : list) {
            if (userInfo.getUsername().equals(name)) {
                if (userInfo.getCount() == 3) {
                    System.out.println("您的账户已被锁定");
                } else {
                    if (userInfo.getPassword().equals(password)) {
                        System.out.println("登录成功");
                        userInfo.setCount(0);
                        flag++;
                    } else {
                        userInfo.setCount(userInfo.getCount() + 1);
                    }
                }
                break;
            }
        }
        if (flag == 0) {
            System.out.println("登陆失败");
        }

        BufferedWriter bw = new BufferedWriter(new FileWriter(file));

        for (UserInfo userInfo : list) {
            bw.write(userInfo.toString());
            bw.newLine();
        }
        bw.close();

    }
}
